package com.zzld.modules.autoinn.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.autoinn.entity.Product;
import com.zzld.modules.autoinn.entity.Store;
import com.zzld.modules.autoinn.service.IProductService;
import com.zzld.modules.common.dto.CommonJSON;

@Controller
@RequestMapping("/autoinn/product")
public class ProductController {
	
	@Autowired
	private IProductService productService;

	private String listurl="/modules/autoinn/product/productList";
	private String cruurl="/modules/autoinn/product/productCru";
	
	@RequestMapping(method=RequestMethod.GET)
	public String list(Model model,HttpServletRequest request){
		return listurl;
	}
	
	@RequestMapping(value="update/pro")
	public String cru(Model model,HttpServletRequest request){
		return cruurl;
	}
	
	@RequestMapping(value="update/pro/json",method=RequestMethod.POST)
	@ResponseBody
	public CommonJSON update(Model model,HttpServletRequest request,
			Product pro){
		CommonJSON json = new CommonJSON();
		
		if(StringUtils.isBlank(pro.getId())){
			//新增
			pro.setId(UuidUtil.uuid().toString());
			pro.setCreate_date(new Date());
			try{
				productService.insertpro(pro);
				json.setMessage(MessageUtils.ADD_SUCCESS);
				json.setSuccess(true);
			}catch(Exception e){
				e.printStackTrace();
				json.setMessage(MessageUtils.ADD_FAIL);
				json.setSuccess(false);
			}
		}else{
			//修改
			try{
				productService.updateByPrimaryKeySelective(pro);
				json.setMessage(MessageUtils.EDIT_SUCCESS);
				json.setSuccess(true);
			}catch(Exception e){
				e.printStackTrace();
				json.setMessage(MessageUtils.EDIT_WARING);
				json.setSuccess(false);
			}
		}
		
		
		return json;
	}
	
	@RequestMapping(value="delete/{id}",method=RequestMethod.POST)
	@ResponseBody
	public CommonJSON del(Model model,HttpServletRequest request,
			@PathVariable("id") String id){
		CommonJSON json = new CommonJSON();
	    try{
	    
	        int e = 	productService.deleteByPrimaryKey(id);
	    	json.setMessage(MessageUtils.DELETE_SUCCESS);
	    	json.setSuccess(true);
	    }catch(Exception e){
	    	e.printStackTrace();
	    	json.setMessage(MessageUtils.DELETE_WARNING);
	    	json.setSuccess(false);
	    }
		
		return json;
	}
	
	@RequestMapping(value="list")
	@ResponseBody
	public CommonJSON lists(Model model,HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
	
		map.put("name", request.getParameter("name"));
		map.put("seaproname", request.getParameter("seaproname"));
		map.put("path", request.getParameter("seacpath"));
		map.put("areaid", request.getParameter("areaid"));
		map.put("sort", "create_date");
		map.put("order", request.getParameter("order"));
		
		Integer PageNum = Integer.parseInt(request.getParameter("page"));
		Integer PageSize = Integer.parseInt(request.getParameter("rows"));
		
		CommonJSON json = new CommonJSON();
		PageInfo pageinfo =   productService.list(map, PageNum, PageSize);
		json.setRows(pageinfo.getList());
		json.setTotal(pageinfo.getTotal());
		return json;
	}
	
	@RequestMapping(value="update/pro/ser",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject ser(Model model,HttpServletRequest request){
		JSONObject json = new JSONObject();
		String id = request.getParameter("id");
		Product s= null;
		try {
			s = productService.selectid(id);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("store_id",s.getStore_id());
			map.put("proname",s.getProname());
			map.put("path", s.getPath());
			map.put("yprice", s.getYprice());
			map.put("xprice", s.getXprice());
			map.put("id", s.getId());
			json.putAll(map);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		return json;
	}
	
}
